﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using Microsoft.Reporting.WebForms;
namespace VSM
{


    public partial class QuickInspection : System.Web.UI.Page
    {

        private static string GeneralID = "";
        private static string dbType;
        private static DataTable dtDetail;
        private static int QIID = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
           
            
            if (!IsPostBack) {
                addComboExterior();
                dbType = Session[VSM.Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? VSM.Utils.AppConstants.VSM : VSM.Utils.AppConstants.DEALER;
                GeneralID = Request.QueryString["GeneralID"] == null ? "" : Request.QueryString["GeneralID"];
                hf_GeneralID.Value = GeneralID;
                loadDataByServiceID();
                loadQuickInspection();
               
            }
          
        }

        private string FormatDate(string dateIn)
        {
            if (dateIn.Trim() == "")
                return "";
            DateTime pars = DateTime.Parse(dateIn);
            return pars.ToString("dd/MM/yyyy");

        }

        private void addComboExterior() {
            DataTable tb = new DataTable();
            tb.Columns.Add("Code");
            tb.Columns.Add("Name");

            tb.Rows.Add("0", "None");
            tb.Rows.Add("1", "Dent");
            tb.Rows.Add("2", "Scratch");
            tb.Rows.Add("3", "Paint Defect");
            tb.Rows.Add("4", "Crack");
            tb.Rows.Add("5", "Rust");
            tb.Rows.Add("6", "Buuble");

            ddl_Exterior.DataSource = tb;
            ddl_Exterior.DataValueField = "Code";
            ddl_Exterior.DataTextField = "Name";
            ddl_Exterior.DataBind();
        }

        private void loadDataByServiceID()
        {
            Services sv = new Services();
            string sql = " select gs.JobID, gs.Receptionist, gs.AdviserCode, gs.TechnicianCode, ";
            sql += " gs.DateIn, gs.TimeIn, gs.ExpDelivery, gs.ExpTime, gs.StartDate, gs.StartTime, ";
            sql += " gs.VehicleCode, gs.VIN, gs.CustomerCode, ";
            sql += " gs.FinishDate, gs.FinishTime, gs.ActualDelivery, gs.ActualDeliveryTime, gs.CustomerRequest, gs.Notice, ";
            sql += " gs.Odometer, gs.ServiceDate, gs.JobKind, ";
            sql += " gs.VehicleName, gs.Brand, gs.EngineNo, gs.RegNo, gs.Model, gs.ColorCode, gs.CustomerName, gs.OtherName, ";
            sql += " gs.Address, gs.Tel, gs.TaxCode, gs.ContactPerson, gs.ContactPhone ";
            sql += " from U_GeneralService gs ";
            sql += " where gs.GeneralID = " + GeneralID.ToString();

            DataSet ds = sv.ExecuteQuery(dbType, sql);
            
            if (ds.Tables[0].Rows.Count > 0)
            {

                DataRow dr = ds.Tables[0].Rows[0];
                txt_Date.Text = FormatDate(dr["ServiceDate"].ToString());
                txt_JobID.Text = dr["JobID"].ToString();
                txt_Odometer.Text = dr["Odometer"].ToString();
                txt_VIN.Text = ds.Tables[0].Rows[0]["VIN"].ToString();
                txt_CustomerName.Text = ds.Tables[0].Rows[0]["CustomerName"].ToString();
                txt_Address.Text = ds.Tables[0].Rows[0]["Address"].ToString();
            }
        }

        private void loadCustomer(string CustomerCode)
        {
            MasterData masterDataWS = new MasterData();
            DataSet ds = masterDataWS.GetCustomerByCode("VSM", CustomerCode);
            if (ds.Tables[0].Rows.Count > 0)
            {
                txt_CustomerName.Text = ds.Tables[0].Rows[0]["CardName"].ToString();
                txt_Address.Text = ds.Tables[0].Rows[0]["Address"].ToString();
            }
        }

        private void loadVehicle(string vehicleCode)
        {
            Services sv = new Services();
            DataSet ds = sv.ExecuteQuery(dbType, "select * from U_ServiceVehicle where VehicleID = " + vehicleCode);
            if (ds.Tables[0].Rows.Count > 0)
            {
                txt_VIN.Text = ds.Tables[0].Rows[0]["VIN"].ToString();
            }
        }

        private void loadQuickInspection()
        {
            if (GeneralID == "")
                return;
            Services sv = new Services();
            dtDetail = sv.ExecuteQuery(dbType, "select * from U_QuickInsp where GeneralID = " + GeneralID).Tables[0];

            if (dtDetail.Rows.Count > 0)
            {

                QIID = Int32.Parse( dtDetail.Rows[0]["QIID"].ToString());
                ddl_Exterior.Items.FindByValue(dtDetail.Rows[0]["Exterior"].ToString()).Selected = true;

                txt_OtherRemark.Text = dtDetail.Rows[0]["OtherRemarks"].ToString();
                chk_NoGoods.Checked = dtDetail.Rows[0]["NoGoods"].ToString() == "0" ? false : true;
                txt_FuelLevel.Text = dtDetail.Rows[0]["FuelLevel"].ToString();

                rbl_1.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["1"].ToString());
                rbl_2.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["2"].ToString());
                rbl_3.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["3"].ToString());
                rbl_4.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["4"].ToString());
                rbl_5.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["5"].ToString());
                rbl_6.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["6"].ToString());
                rbl_7.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["7"].ToString());
                rbl_8.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["8"].ToString());
                rbl_9.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["9"].ToString());
                rbl_10.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["10"].ToString());
                rbl_11.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["11"].ToString());
                rbl_12.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["12"].ToString());
                rbl_13.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["13"].ToString());
                rbl_14.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["14"].ToString());
                rbl_15.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["15"].ToString());
                rbl_16.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["16"].ToString());
                rbl_17.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["17"].ToString());
                rbl_18.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["18"].ToString());
                rbl_19.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["19"].ToString());
                rbl_20.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["20"].ToString()); 
                rbl_21.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["21"].ToString());
                rbl_22.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["22"].ToString());
                rbl_41.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["41"].ToString());
                rbl_231.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["231"].ToString());
                rbl_232.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["232"].ToString());
                rbl_233.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["233"].ToString());
                rbl_234.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["234"].ToString());
                rbl_241.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["241"].ToString());
                rbl_242.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["242"].ToString());
                rbl_25.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["25"].ToString());

                chk_261.Checked = dtDetail.Rows[0]["261"].ToString() == "0" ? false : true;
                chk_262.Checked = dtDetail.Rows[0]["262"].ToString() == "0" ? false : true;
                chk_271.Checked = dtDetail.Rows[0]["271"].ToString() == "0" ? false : true;
                chk_272.Checked = dtDetail.Rows[0]["272"].ToString() == "0" ? false : true;

                rbl_281.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["281"].ToString());
                rbl_282.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["282"].ToString());
                rbl_283.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["283"].ToString());
                rbl_284.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["284"].ToString());
                rbl_285.SelectedIndex = Int16.Parse(dtDetail.Rows[0]["285"].ToString());
                txt_TechnicalComment.Text = dtDetail.Rows[0]["TechnicalComment"].ToString();
            }
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            //ReportDocument rptDoc = new ReportDocument();
            //rptDoc.Load(Server.MapPath("/Business/Service/Jobcard.rpt"));
            //rptDoc.PrintToPrinter(1, false, 0, 0);
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("GeneralID");
            dt.Columns.Add("Exterior");
            dt.Columns.Add("OtherRemarks");
            dt.Columns.Add("NoGoods");
            dt.Columns.Add("FuelLevel");

            dt.Columns.Add("1");
            dt.Columns.Add("2");
            dt.Columns.Add("3");
            dt.Columns.Add("4");
            dt.Columns.Add("5");
            dt.Columns.Add("6");
            dt.Columns.Add("7");
            dt.Columns.Add("8");
            dt.Columns.Add("9");
            dt.Columns.Add("10");
            dt.Columns.Add("11");
            dt.Columns.Add("12");
            dt.Columns.Add("13");
            dt.Columns.Add("14");
            dt.Columns.Add("15");
            dt.Columns.Add("16");
            dt.Columns.Add("17");
            dt.Columns.Add("18");
            dt.Columns.Add("19");
            dt.Columns.Add("20");
            dt.Columns.Add("21");
            dt.Columns.Add("22");
            dt.Columns.Add("41");
            dt.Columns.Add("231");
            dt.Columns.Add("232");
            dt.Columns.Add("233");
            dt.Columns.Add("234");
            dt.Columns.Add("241");
            dt.Columns.Add("242");
            dt.Columns.Add("25");
            dt.Columns.Add("261");
            dt.Columns.Add("262");
            dt.Columns.Add("271");
            dt.Columns.Add("272");
            dt.Columns.Add("281");
            dt.Columns.Add("282");
            dt.Columns.Add("283");
            dt.Columns.Add("284");
            dt.Columns.Add("285");
            dt.Columns.Add("TechnicalComment");
            dt.Columns.Add("QIID");

            DataRow dr = dt.NewRow();
            dr["GeneralID"] = GeneralID;
            dr["Exterior"] = ddl_Exterior.SelectedValue;
            dr["OtherRemarks"] = txt_OtherRemark.Text;
            dr["NoGoods"] = chk_NoGoods.Checked == true ? 1 : 0;
            dr["FuelLevel"] = txt_FuelLevel.Text;

            dr["1"] = rbl_1.SelectedIndex;
            dr["2"] = rbl_2.SelectedIndex;
            dr["3"] = rbl_3.SelectedIndex;
            dr["4"] = rbl_4.SelectedIndex;
            dr["5"] = rbl_5.SelectedIndex;
            dr["6"] = rbl_6.SelectedIndex;
            dr["7"] = rbl_7.SelectedIndex;
            dr["8"] = rbl_8.SelectedIndex;
            dr["9"] = rbl_9.SelectedIndex;
            dr["10"] = rbl_10.SelectedIndex;
            dr["11"] = rbl_11.SelectedIndex;
            dr["12"] = rbl_12.SelectedIndex;
            dr["13"] = rbl_13.SelectedIndex;
            dr["14"] = rbl_14.SelectedIndex;
            dr["15"] = rbl_15.SelectedIndex;
            dr["16"] = rbl_16.SelectedIndex;
            dr["17"] = rbl_17.SelectedIndex;
            dr["18"] = rbl_18.SelectedIndex;
            dr["19"] = rbl_19.SelectedIndex;
            dr["20"] = rbl_20.SelectedIndex;
            dr["21"] = rbl_21.SelectedIndex;
            dr["22"] = rbl_22.SelectedIndex;
            dr["41"] = rbl_41.SelectedIndex;
            dr["231"] = rbl_231.SelectedIndex;
            dr["232"] = rbl_232.SelectedIndex;
            dr["233"] = rbl_233.SelectedIndex;
            dr["234"] = rbl_234.SelectedIndex;
            dr["241"] = rbl_241.SelectedIndex;
            dr["242"] = rbl_242.SelectedIndex;
            dr["25"] = rbl_25.SelectedIndex;
            dr["261"] = chk_261.Checked == true ? 1 : 0;
            dr["262"] = chk_262.Checked == true ? 1 : 0;
            dr["271"] = chk_271.Checked == true ? 1 : 0;
            dr["272"] = chk_272.Checked == true ? 1 : 0;
            dr["281"] = rbl_281.SelectedIndex;
            dr["282"] = rbl_282.SelectedIndex;
            dr["283"] = rbl_283.SelectedIndex;
            dr["284"] = rbl_284.SelectedIndex;
            dr["285"] = rbl_285.SelectedIndex;
            dr["TechnicalComment"] = txt_TechnicalComment.Text;
            dr["QIID"] = QIID;

            dt.Rows.Add(dr);

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            Services sv = new Services();
           

            if (QIID > -1) //update
            {
                int count = sv.UpdateQuickInspection(dbType, ds);
                if (count != -1)
                    lb_Message.Text = "[Update] Operation complete.";
                else
                    lb_Message.Text = "[Update] Operation failure.";
            }
            else
            { //add new

                QIID = sv.InsertQuickInspection(dbType, ds);
                if (QIID != -1)
                {
                    lb_Message.Text = "[Add New] Operation complete.";
                }
                else
                    lb_Message.Text = "[Add New] Operation failure.";
            }
        }
      
    }
}
